/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-22
 * Time: 06:52
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using tools;

using Labb4.Main;

namespace Labb4.Pooman.Players
{
	/// <summary>
	/// Holds the active players in the current round
	/// </summary>
	public class PlayerSwarm
	{
		#region members
		
		/// <summary>
		/// References to all players in this swarm
		/// </summary>
		private List<Player>		m_Players;
		/// <summary>
		/// The number of players at the start of the round
		/// </summary>
		private int					m_NumberOfPlayersAtStartOfRound;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members
		/// </summary>
		public PlayerSwarm()
		{
			m_Players = new List<Player>();
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Adds a player to the swarm, and updates the value in CountAtStartOfRound
		/// This property will now hold the number of players in the member collection
		/// at the end of this method
		/// </summary>
		/// <param name="player">Reference to the player to add</param>
		public void AddPlayer(Player player)
		{
			m_Players.Add(player);
			m_NumberOfPlayersAtStartOfRound = m_Players.Count;
		}
		
		/// <summary>
		/// Gets the next player of the swarm, and removes this player from the swarm.
		/// If there's no players in the swarm, null is returned.
		/// </summary>
		/// <returns>The next player, null if member collection is empty</returns>
		public Player GetNextPlayer()
		{
			Player next = null;
			
			if(m_Players.Count > 0)
			{
				next = m_Players[0];
				m_Players.RemoveAt(0);
			}
			else
			{
				MessageBox.Show("Oh oh..");
			}
			
			return next;
		}
		
		/// <summary>
		/// Removes a player from the swarm, if that player is in the member collection
		/// </summary>
		/// <param name="player">Reference to the player to remove</param>
		public void RemovePlayer(Player player)
		{
			if(m_Players.Contains(player))
			{
				m_Players.Remove(player);
			}
		}
		
		/// <summary>
		/// Clears the swarm, and resets it to its initial state
		/// </summary>
		public void Clear()
		{
			m_Players.Clear();
			m_NumberOfPlayersAtStartOfRound = 0;
		}
		
		/// <summary>
		/// Saves the swarm to a List of string
		/// </summary>
		/// <param name="fileContent">The List of string to save the swarm to</param>
		public void Save(List<string> fileContent)
		{
			fileContent.Add("players_at_start_of_round " + m_NumberOfPlayersAtStartOfRound.ToString());
			fileContent.Add("players_in_swarm " + m_Players.Count.ToString());
			int player = 0;
			
			foreach(Player it in m_Players)
			{
				fileContent.Add("swarm_player_" + player.ToString() + " " + PlayerManager.Instance[it].ToString());
				player++;
			}
		}
		
		/// <summary>
		/// Loads a swarm from a file
		/// </summary>
		/// <param name="config">The file content to load the swarm from</param>
		public void Load(Config config)
		{
			m_Players.Clear();
			int players = 0;
			
			if(MainForm.ErrorCheck(config.GetInt("players_at_start_of_round", ref m_NumberOfPlayersAtStartOfRound)) &&
			   MainForm.ErrorCheck(config.GetInt("players_in_swarm", ref players)))
			{
				int player = 0;
				
				for(int i = 0; i < players; i++)
				{
					if(MainForm.ErrorCheck(config.GetInt("swarm_player_" + i.ToString(), ref player)))
					{
						m_Players.Add(PlayerManager.Instance[player]);
					}
				}
			}
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// The number of players in the swarm (get)
		/// </summary>
		public int Count
		{
			get
			{
				return m_Players.Count;
			}
		}
		
		/// <summary>
		/// The number of players in the swarm after the last time AddPlayer was called (get)
		/// </summary>
		public int CountAtStartOfRound
		{
			get
			{
				return m_NumberOfPlayersAtStartOfRound;
			}
		}
		
		#endregion
	}
}
